package com.zx.sys.cms.api.admin;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.common.utils.Constant;
import com.zx.common.utils.PageUtils;
import com.zx.common.utils.R;
import com.zx.common.utils.ShiroUtils;
import com.zx.sys.cms.entity.CmsChannelEntity;
import com.zx.sys.cms.service.CmsChannelService;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author LiYe
 * @email sunlightcs@gmail.com
 * @date 2020-03-09 16:15:28
 */
@RestController
@RequestMapping("/sys/cmschannel")
public class CmsChannelApi {
    @Autowired
    private CmsChannelService cmsChannelService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:cmschannel:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = cmsChannelService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:cmschannel:info")
    public R info(@PathVariable("id") Long id) {
        CmsChannelEntity cmsChannel = cmsChannelService.getById(id);
        return R.ok().put("info", cmsChannel);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:cmschannel:save")
    public R save(@RequestBody @ApiParam(name = "对象", value = "传入json格式", required = true) CmsChannelEntity cmsChannel) {
        try {
            Integer type = cmsChannel.getType();
            Long userId = ShiroUtils.getUserId();

            cmsChannel.setCreator(userId);
            cmsChannel.setUpdater(userId);
            cmsChannel.setCreateTime(new Date());
            cmsChannel.setUpdateTime(new Date());

            if (type != null && type.equals(Constant.CmsContentType.image.getValue())) {
                JSONArray pictureArr = cmsChannel.getPicturesArray();
                cmsChannel.setPictures(pictureArr.toJSONString());
                if (pictureArr != null && pictureArr.size() > 0) {
                    cmsChannel.setPicture(pictureArr.getJSONObject(0).getString("url"));
                }
            } else {
                cmsChannel.setPictures(null);
            }
            cmsChannelService.save(cmsChannel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:cmschannel:update")
    public R update(@RequestBody CmsChannelEntity cmsChannel) {
        Integer isContent = cmsChannel.getIsContent();
        Integer type = cmsChannel.getType();

        if(!isContent.equals(1)){
            cmsChannel.setPictures(null);
            cmsChannel.setMedia(null);
            cmsChannel.setContent(null);
        }else{
            if(type.equals(Constant.CmsContentType.imageText.getValue())){
                cmsChannel.setPictures(null);
                cmsChannel.setMedia(null);
            }else if(type.equals(Constant.CmsContentType.image.getValue())){
                JSONArray pictureArr = cmsChannel.getPicturesArray();
                cmsChannel.setPictures(pictureArr.toJSONString());
                if (pictureArr != null && pictureArr.size() > 0) {
                    cmsChannel.setPicture(pictureArr.getJSONObject(0).getString("url"));
                }
                cmsChannel.setMedia(null);
            }else {
                cmsChannel.setPictures(null);
                cmsChannel.setMedia(null);
            }
        }
        cmsChannel.setUpdateTime(new Date());
        cmsChannel.setUpdater(ShiroUtils.getUserId());
        cmsChannelService.updateById(cmsChannel);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:cmschannel:delete")
    public R delete(@RequestBody Long[] ids) {
        cmsChannelService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @RequestMapping("/alllist")
    @RequiresPermissions("sys:cmschannel:list")
    public R getAllList(HttpServletRequest request){
        QueryWrapper<CmsChannelEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_content", 2);
        queryWrapper.eq("is_display",1);
        queryWrapper.orderByAsc("priority");
        List<CmsChannelEntity> list = cmsChannelService.list(queryWrapper);
        return R.ok().put("info", list);
    }

}
